<?php

require_once dirname(__FILE__) . '/EventStaff.php';
require_once dirname(__FILE__) . '/../event/EventDAL.php';

class StaffModel {
	private $database;

	private static $k_event_staff = 'event_staff';
	private static $k_username = 'username';
	private static $k_visitor_event_id = 'visitor_event_id';
	private static $k_staff_role = 'staff_role';
	private static $k_confirmed = 'confirmed';
	private static $k_submitted = 'submitted';

	public function __construct(Database $db) {
		$this->database = $db;
	}

	public function submitStaffApplication(EventStaff $eventStaff) {
		$sql = 'INSERT INTO ' . self::$k_event_staff . ' (' . self::$k_username . ', ' . self::$k_visitor_event_id . ', ' . self::$k_staff_role . ', ' . self::$k_confirmed . ', ' . self::$k_submitted . ') VALUES (?, ?, ?, ?, NOW())';
		$stmt = $this->database->prepare($sql);
		$stmt->bind_param("sssi", $eventStaff->username, $eventStaff->visitorEventId, $eventStaff->staffRole, $eventStaff->confirmed);
		$this->database->ExecuteNonQuery($stmt);
	}

	public function getStaffApplications() {
		$eventStaff = array();
		$sql = 'SELECT ' . self::$k_username . ', ' . self::$k_event_staff . '.' . self::$k_visitor_event_id . ' ,' . EventDAL::getStartDateColumn() . ', ' . self::$k_staff_role . ', ' . self::$k_submitted . ' FROM ' . self::$k_event_staff . ' JOIN ' . EventDAL::getEventTable() . ' ON ' . self::$k_event_staff . '.' . self::$k_visitor_event_id . ' = ' . EventDAL::getEventTable() . '.' . EventDAL::getVisitorEventIdColumn() . ' WHERE `' . self::$k_confirmed . '` = 0 AND ' . EventDAL::getStartDateColumn() . ' > NOW() ORDER BY ' . EventDAL::getStartDateColumn() . ', ' . self::$k_submitted . '';
		$stmt = $this->database->prepare($sql);
		$stmt->execute();

		$stmt->bind_result($username, $visitorEventId, $visitorEventStart, $staffRole, $submitted);
    	
    	while ($stmt->fetch()) {
    		$eventStaff[] = EventStaff::create($username, $visitorEventId, $visitorEventStart, $staffRole, false, $submitted);
    	}

    	return $eventStaff;
	}
}